/**
 * @desc 过滤方法
 * @author xingzitao
 * @lastTime 2022-02-26
 */

import dayjs from 'dayjs'

export const date = (d, fmt = 'YYYY-MM-DD') => {
  return dayjs(new Date(d)).format(fmt)
}

export const time = (d, fmt = 'YYYY-MM-DD HH:mm:ss') => {
  return date(d, fmt)
}

export const minute = (d, fmt = 'YYYY-MM-DD HH:mm') => {
  return date(d, fmt)
}

export const second = (d, fmt = 'HH:mm:ss') => {
  return date(d, fmt)
}

export const month = (d, fmt = 'YYYY-MM') => {
  return date(d, fmt)
}

export const year = (d, fmt = 'YYYY') => {
  return date(d, fmt)
}

// 千分符，整数
export const thousands = (num) => {
  if (typeof num !== 'number' && !num) {
    return ''
  }
  return (+num).toLocaleString()
}

// 千分符，格式两位小数
export const decimalpre = (num, precision = 2) => {
  if (typeof num !== 'number' && !num) {
    return ''
  }
  return `${(+num).toFixed(precision).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')}`
}

